package cn.psh.org.controller;


import cn.psh.basic.util.PageList;
import cn.psh.basic.util.AjaxResult;
import cn.psh.basic.util.annotation.PreAuthorize;
import cn.psh.org.domain.Employee;
import cn.psh.org.query.EmployeeQuery;
import cn.psh.org.service.impl.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/emp")
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeServiceImpl employeeService;



    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }

    /**
     * 查询所有
     */
    @GetMapping
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (null == employee.getId()) {
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.OJBK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setMessage("系统错误，正在殴打程序员！！骚等");
        }
    }

    /**
     * 根据id删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.del(id);
            return AjaxResult.OJBK();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.OJBK().setMessage("系统错误，正在殴打程序员！！骚等");
    }

    /**
     * 根据ids批量删除
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            employeeService.batchDel(ids);
            return AjaxResult.OJBK();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.OJBK().setMessage("系统错误，正在殴打程序员！！骚等");
    }

    /**
     * 分页查询
     */
    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
        return employeeService.queryData(query);
    }

    @GetMapping("/list/{id}")
    public List<Employee> listyg(@PathVariable("id") Long id){
        return employeeService.listyg(id);
    }

}
